package gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import metodologia.Etapa;

public class JanelaEtapas extends javax.swing.JFrame implements java.io.Serializable{

    private int r, c;
    private ArrayList<JCheckBox> boxes;
    private Etapa etapa;
    private ArrayList<Etapa> listaDeEtapas;
    private boolean selecionado;
    private int checkBoxSelecionado;

    /** Creates new form toDo */
    public JanelaEtapas() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setTitle("PWManager - Etapas");
        ImageIcon imagemTituloJanela = new ImageIcon(getClass().getResource("/ImageFiles/icon_PW.png"));  
        setIconImage(imagemTituloJanela.getImage());
        setSize(500, 500);
        listaDeEtapas = new ArrayList<Etapa>();
        r = 1;
        c = 1;
        boxes = new ArrayList<JCheckBox>();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        checkBoxSelecionado = -1;
        selecionado = false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jbAdicionar = new javax.swing.JButton();
        jbEditar = new javax.swing.JButton();
        jbRemover = new javax.swing.JButton();
        jbSubir = new javax.swing.JButton();
        jbDescer = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jmSair = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jmAdicionar = new javax.swing.JMenuItem();
        jmEditar = new javax.swing.JMenuItem();
        jmRemover = new javax.swing.JMenuItem();
        jmMoverPraCima = new javax.swing.JMenuItem();
        jmMoverPraBaixo = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(500, 500));

        jScrollPane1.setMinimumSize(new java.awt.Dimension(300, 300));

        jPanel2.setLayout(new java.awt.GridLayout(0, 1));
        jScrollPane1.setViewportView(jPanel2);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setPreferredSize(new java.awt.Dimension(378, 55));

        jbAdicionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/adicionarEtapa.png"))); // NOI18N
        jbAdicionar.setText("Adicionar");
        jbAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAdicionarActionPerformed(evt);
            }
        });
        jPanel1.add(jbAdicionar);

        jbEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/editarEtapa.png"))); // NOI18N
        jbEditar.setText("Editar");
        jbEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEditarActionPerformed(evt);
            }
        });
        jPanel1.add(jbEditar);

        jbRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/removerEtapa.png"))); // NOI18N
        jbRemover.setText("Remover");
        jbRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbRemoverActionPerformed(evt);
            }
        });
        jPanel1.add(jbRemover);

        jbSubir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/subir.png"))); // NOI18N
        jbSubir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSubirActionPerformed(evt);
            }
        });
        jPanel1.add(jbSubir);

        jbDescer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/descer.png"))); // NOI18N
        jbDescer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDescerActionPerformed(evt);
            }
        });
        jPanel1.add(jbDescer);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        jMenu1.setText("Arquivo");

        jmSair.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, 0));
        jmSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/sair16x16.png"))); // NOI18N
        jmSair.setText("Sair");
        jmSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmSairActionPerformed(evt);
            }
        });
        jMenu1.add(jmSair);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Editar");

        jmAdicionar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        jmAdicionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/adicionarEtapa16x16.png"))); // NOI18N
        jmAdicionar.setText(" Adicionar Etapa");
        jmAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmAdicionarActionPerformed(evt);
            }
        });
        jMenu2.add(jmAdicionar);

        jmEditar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        jmEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/editarEtapa16x16.png"))); // NOI18N
        jmEditar.setText("Editar Etapa");
        jmEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmEditarActionPerformed(evt);
            }
        });
        jMenu2.add(jmEditar);

        jmRemover.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
        jmRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/removerEtapa16x16.png"))); // NOI18N
        jmRemover.setText("Remover Etapa");
        jmRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmRemoverActionPerformed(evt);
            }
        });
        jMenu2.add(jmRemover);

        jmMoverPraCima.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_UP, 0));
        jmMoverPraCima.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/subir16x16.png"))); // NOI18N
        jmMoverPraCima.setText("Mover para cima");
        jmMoverPraCima.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmMoverPraCimaActionPerformed(evt);
            }
        });
        jMenu2.add(jmMoverPraCima);

        jmMoverPraBaixo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DOWN, 0));
        jmMoverPraBaixo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ImageFiles/descer16x16.png"))); // NOI18N
        jmMoverPraBaixo.setText("Mover para baixo");
        jmMoverPraBaixo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmMoverPraBaixoActionPerformed(evt);
            }
        });
        jMenu2.add(jmMoverPraBaixo);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void zerarCheckBox(){
        int cont = 0;
        for(Iterator i = boxes.iterator(); i.hasNext();){
            JCheckBox j = (JCheckBox)i.next();
            if(cont != checkBoxSelecionado)j.setSelected(false);
            cont++;
        }
    }
    
    public void setarEtapa(Etapa etapa, int indice){
        // Índice = -1 : Nova Etapa
        // indice != -1 : Editar Etapa
        if(indice == -1){
            this.etapa = etapa;
            if(!"".equals(etapa.getTitulo())){
                JCheckBox active = new JCheckBox(etapa.getTitulo());
                
                JTextField text;
                if("".equals(etapa.getDescricao())){ 
                    text = new JTextField("Sem descrição");
                }else {
                    text = new JTextField(etapa.getDescricao());
                }
                
                text.setEditable(false);
                boxes.add(active);
                active.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        checkBoxSelecionado = boxes.indexOf((JCheckBox)e.getSource());
                        if(boxes.get(checkBoxSelecionado).isSelected()){
                            selecionado = true;
                        }else{
                            System.out.println("entrou aqui");
                            checkBoxSelecionado = -1;
                            selecionado = false;
                        }
                        zerarCheckBox();
                    }
                });
                this.jPanel2.setLayout(new GridLayout((15 + r++), c));
                this.jPanel2.add(boxes.get(boxes.size() - 1));
                this.jPanel2.add(text);
                this.jPanel2.doLayout();
                this.jScrollPane1.doLayout();
                this.doLayout();
                this.jPanel2.revalidate();
                this.jPanel2.repaint();
                listaDeEtapas.add(etapa); 
            }
        }else{
            getListaDeEtapas().get(indice).setDados(etapa.getTitulo(), etapa.getDescricao(), etapa.isPlanilha(), 
                    etapa.getnLinhas(), etapa.getnColunas());
            boxes.get(indice).setText(etapa.getTitulo()); 

            int a = 0;
            jPanel2.removeAll();
            System.out.println("N checkbox: " + boxes.size() + " N Lista: " + listaDeEtapas.size());
            for(Iterator i = boxes.iterator(); i.hasNext();){
                JCheckBox active1 = (JCheckBox)i.next();
                
                JTextField text1;
                if("".equals(listaDeEtapas.get(a).getDescricao())){ 
                    text1 = new JTextField("Sem descrição");
                }else {
                    text1 = new JTextField(listaDeEtapas.get(a).getDescricao());
                }
                text1.setEditable(false);
                this.jPanel2.setLayout(new GridLayout((15 + r), c));
                this.jPanel2.add(active1);
                this.jPanel2.add(text1);
                this.jPanel2.doLayout();
                this.jScrollPane1.doLayout();
                this.doLayout();
                a++;
            }
            this.jPanel2.revalidate();
            this.jPanel2.repaint();
        }
    }
    
    
    private void jbAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAdicionarActionPerformed
        adicionarEtapa();
    }//GEN-LAST:event_jbAdicionarActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jmSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_jmSairActionPerformed

    private void jmAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmAdicionarActionPerformed
        adicionarEtapa();
    }//GEN-LAST:event_jmAdicionarActionPerformed

    private void jbRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbRemoverActionPerformed
        removerEtapa();
    }//GEN-LAST:event_jbRemoverActionPerformed

    private void jmRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmRemoverActionPerformed
        removerEtapa();
    }//GEN-LAST:event_jmRemoverActionPerformed

    private void jbEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEditarActionPerformed
        editarEtapa();
    }//GEN-LAST:event_jbEditarActionPerformed

    private void jbSubirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSubirActionPerformed
        alterarPosicao(true);
    }//GEN-LAST:event_jbSubirActionPerformed

    private void jbDescerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbDescerActionPerformed
        alterarPosicao(false);
    }//GEN-LAST:event_jbDescerActionPerformed

    private void jmMoverPraCimaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmMoverPraCimaActionPerformed
        alterarPosicao(true);
    }//GEN-LAST:event_jmMoverPraCimaActionPerformed

    private void jmMoverPraBaixoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmMoverPraBaixoActionPerformed
        alterarPosicao(false);
    }//GEN-LAST:event_jmMoverPraBaixoActionPerformed

    private void jmEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmEditarActionPerformed
        editarEtapa();
    }//GEN-LAST:event_jmEditarActionPerformed

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAdicionar;
    private javax.swing.JButton jbDescer;
    private javax.swing.JButton jbEditar;
    private javax.swing.JButton jbRemover;
    private javax.swing.JButton jbSubir;
    private javax.swing.JMenuItem jmAdicionar;
    private javax.swing.JMenuItem jmEditar;
    private javax.swing.JMenuItem jmMoverPraBaixo;
    private javax.swing.JMenuItem jmMoverPraCima;
    private javax.swing.JMenuItem jmRemover;
    private javax.swing.JMenuItem jmSair;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the listaDeEtapas
     */
    public ArrayList<Etapa> getListaDeEtapas() {
        return listaDeEtapas;
    }

    /**
     * @param listaDeEtapas the listaDeEtapas to set
     */
    public void setListaDeEtapas(ArrayList<Etapa> listaDeEtapas) {
        this.listaDeEtapas = listaDeEtapas;
    }
    
    public void carregarListaDeCheckBox(){
        for(Iterator i = listaDeEtapas.iterator(); i.hasNext();){
            Etapa e = (Etapa)i.next();
            JCheckBox active = new JCheckBox(e.getTitulo());
            active.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        checkBoxSelecionado = boxes.indexOf((JCheckBox)e.getSource());
                        if(boxes.get(checkBoxSelecionado).isSelected()){
                            selecionado = true;
                        }else{
                            System.out.println("entrou aqui");
                            checkBoxSelecionado = -1;
                            selecionado = false;
                        }
                        zerarCheckBox();
                    }
                });
            boxes.add(active);
        }
        int a = 0;
        jPanel2.removeAll();
        for(Iterator i = boxes.iterator(); i.hasNext();){
            JCheckBox active1 = (JCheckBox)i.next();
            JTextField text1;
            if("".equals(listaDeEtapas.get(a).getDescricao())){ 
                text1 = new JTextField("Sem descrição");
            }else {
                text1 = new JTextField(listaDeEtapas.get(a).getDescricao());
            }
            text1.setEditable(false);
            this.jPanel2.setLayout(new GridLayout((15 + r), c));
            this.jPanel2.add(active1);
            this.jPanel2.add(text1);
            this.jPanel2.doLayout();
            this.jScrollPane1.doLayout();
            this.doLayout();
            a++;
        }
        this.jPanel2.revalidate();
        this.jPanel2.repaint();
        checkBoxSelecionado++;
    }

    private void alterarPosicao(boolean b) {
        if(b){
            if(checkBoxSelecionado > 0){
                Etapa eAcima = listaDeEtapas.get(checkBoxSelecionado-1);
                Etapa eAtual = listaDeEtapas.get(checkBoxSelecionado);
                listaDeEtapas.set(checkBoxSelecionado-1, eAtual);
                listaDeEtapas.set(checkBoxSelecionado, eAcima);

                JCheckBox jAcima = boxes.get(checkBoxSelecionado-1);
                JCheckBox jAtual = boxes.get(checkBoxSelecionado);
                boxes.set(checkBoxSelecionado-1, jAtual);
                boxes.set(checkBoxSelecionado, jAcima);
                
                int a = 0;
                jPanel2.removeAll();
                for(Iterator i = boxes.iterator(); i.hasNext();){
                    JCheckBox active1 = (JCheckBox)i.next();
                    JTextField text1;
                    if("".equals(listaDeEtapas.get(a).getDescricao())){ 
                        text1 = new JTextField("Sem descrição");
                    }else {
                        text1 = new JTextField(listaDeEtapas.get(a).getDescricao());
                    }
                    text1.setEditable(false);
                    this.jPanel2.setLayout(new GridLayout((15 + r), c));
                    this.jPanel2.add(active1);
                    this.jPanel2.add(text1);
                    this.jPanel2.doLayout();
                    this.jScrollPane1.doLayout();
                    this.doLayout();
                    a++;
                }
                this.jPanel2.revalidate();
                this.jPanel2.repaint();
                checkBoxSelecionado--;
            }
        }else{
            if((checkBoxSelecionado < boxes.size()-1) && (checkBoxSelecionado != -1)){
                Etapa eAbaixo = listaDeEtapas.get(checkBoxSelecionado+1);
                Etapa eAtual = listaDeEtapas.get(checkBoxSelecionado);
                listaDeEtapas.set(checkBoxSelecionado+1, eAtual);
                listaDeEtapas.set(checkBoxSelecionado, eAbaixo);

                JCheckBox jAbaixo = boxes.get(checkBoxSelecionado+1);
                JCheckBox jAtual = boxes.get(checkBoxSelecionado);
                boxes.set(checkBoxSelecionado+1, jAtual);
                boxes.set(checkBoxSelecionado, jAbaixo);
                
                int a = 0;
                jPanel2.removeAll();
                for(Iterator i = boxes.iterator(); i.hasNext();){
                    JCheckBox active1 = (JCheckBox)i.next();
                    JTextField text1;
                    if("".equals(listaDeEtapas.get(a).getDescricao())){ 
                        text1 = new JTextField("Sem descrição");
                    }else {
                        text1 = new JTextField(listaDeEtapas.get(a).getDescricao());
                    }
                    text1.setEditable(false);
                    this.jPanel2.setLayout(new GridLayout((15 + r), c));
                    this.jPanel2.add(active1);
                    this.jPanel2.add(text1);
                    this.jPanel2.doLayout();
                    this.jScrollPane1.doLayout();
                    this.doLayout();
                    a++;
                }
                this.jPanel2.revalidate();
                this.jPanel2.repaint();
                checkBoxSelecionado++;
            }
        }
    }

    private void adicionarEtapa() {
        etapa = new Etapa();
        JanelaDeDadosDasEtapas j = new JanelaDeDadosDasEtapas(etapa, this);
        j.setVisible(true);
        checkBoxSelecionado = -1;
        zerarCheckBox();
    }

    private void editarEtapa() {
        if(!selecionado){
            NewOkCancelDialog n1 = new NewOkCancelDialog(this, true, "Nenhum checkbox selecionado! por favor, selecione algum para poder editá-lo.");
            n1.setVisible(true);
        }else{
            JanelaDeDadosDasEtapasEditar j = new JanelaDeDadosDasEtapasEditar(listaDeEtapas.get(checkBoxSelecionado), this);
            j.setDados(listaDeEtapas.get(checkBoxSelecionado).getTitulo(), listaDeEtapas.get(checkBoxSelecionado).getDescricao(), 
                    listaDeEtapas.get(checkBoxSelecionado).isPlanilha(), listaDeEtapas.get(checkBoxSelecionado).getnLinhas(), 
                    listaDeEtapas.get(checkBoxSelecionado).getnColunas(), checkBoxSelecionado);
            
            System.out.println("colunas: " + listaDeEtapas.get(checkBoxSelecionado).getnColunas());
            j.setVisible(true);
        }
    }

    private void removerEtapa() {
        if(selecionado){
            NewOkCancelDialog n = new NewOkCancelDialog(this, true, "Deseja remover a etapa selecionada?");
            n.setVisible(true);
            int retorno = n.getReturnStatus();
            if(retorno == 1){
                boxes.remove(checkBoxSelecionado);

                int a = 0;
                jPanel2.removeAll();
                System.out.println("N checkbox: " + boxes.size() + " N Lista: " + listaDeEtapas.size());
                for(Iterator i = boxes.iterator(); i.hasNext();){
                    JCheckBox active = (JCheckBox)i.next();
                    JTextField text;

                    if("".equals(listaDeEtapas.get(a).getDescricao())){ 
                        text = new JTextField("Sem descrição");
                    }else {
                        text = new JTextField(listaDeEtapas.get(a).getDescricao());
                    }

                    text.setEditable(false);
                    this.jPanel2.setLayout(new GridLayout((15 + r), c));
                    this.jPanel2.add(active);
                    this.jPanel2.add(text);
                    this.jPanel2.doLayout();
                    this.jScrollPane1.doLayout();
                    this.doLayout();
                    a++;
                }
                this.jPanel2.revalidate();
                this.jPanel2.repaint();
                checkBoxSelecionado = -1;
                zerarCheckBox();
            }
        }else{
            NewOkCancelDialog n1 = new NewOkCancelDialog(this, true, "Nenhum checkbox selecionado! por favor, selecione algum para poder removê-lo.");
            n1.setVisible(true);
        }
        
    }
    
    public void setTitulos(ArrayList<String> titulos){
        etapa.setTitulos(titulos);
    }
}
